package com.yumis.service.modules.car.controller;

import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.Car;
import com.yumis.service.modules.car.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * 查询所有车辆信息（根据id由小大排序）
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("queryCar")
    public ResponseEntity<Response> queryCar(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "rows", defaultValue = "10") Integer rows){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,carService.queryCar(page,rows)));
    }
    /**
     * 修改车辆信息
     * @param car
     * @return
     */
    @PostMapping("updateCar")
    public ResponseEntity<Response> updateCar(@Valid Car car){
        carService.updateCar(car);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 根据id删除车辆信息
     * @param id
     * @return
     */
    @PostMapping("deleteByIdCar")
    public ResponseEntity<Response> deleteByIdCar(@RequestParam("id") Long id){
        carService.deleteByIdCar(id);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 根据id批量删除车辆信息
     * @param ids
     * @return
     */
    @PostMapping("deleteCar")
    public ResponseEntity<Response> deleteByIdCar(@RequestParam("id") Long[] ids){
        carService.deleteCar(ids);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 新增车辆信息
     * @param car
     * @return
     */
    @PostMapping("addCar")
    public ResponseEntity<Response> addCar(@Valid Car car){
        carService.addCar(car);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 根据用户id查询车辆信息
     * @param userId
     * @return
     */
    @PostMapping("queryByUserCar")
    public ResponseEntity<Response> queryByUserCar(@RequestParam("userId") Long userId){
        List<Car> carList = carService.queryByUserCar(userId);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,carList));
    }

}
